package com.nercel.dsj.gksales.util.sms.shiyan;

import java.time.Instant;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;

/**
 * 十堰短信平台发送短信工具类
 * 
 * @author qsx
 * @date 2020-09-04 17:25:00
 */
@Component
@SuppressWarnings("unchecked")
public class ShiyanSmsUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${shiyanSms.sendUrl}")
    private String sendUrl;

    @Value("${shiyanSms.stateUrl}")
    private String stateUrl;

    @Value("${shiyanSms.moUrl}")
    private String moUrl;

    @Value("${shiyanSms.userId}")
    private String userId;

    @Value("${shiyanSms.password}")
    private String password;

    @Value("${shiyanSms.signTemplate}")
    private String signTemplate;

    /**
     * 发送短信
     * 
     * @param to      被叫号码
     * @param content 短信内容
     */

    public void sendSms(String to, String content) {
	String timestamp = Instant.now().toEpochMilli() + "";
	sendUrl = sendUrl + "?userId={userId}&sign={sign}&mobile={mobile}&content={content}&timestamp={timestamp}";
	Map<String, Object> result = restTemplate.postForObject(sendUrl, null, Map.class, userId, sign(timestamp), to,
		content, timestamp);
	System.out.println(result);
    }

    /**
     * 状态报告
     */
    public void queryStatus() {
	String timestamp = Instant.now().toEpochMilli() + "";
	stateUrl = stateUrl + "?userId={userId}&sign={sign}&timestamp={timestamp}";
	List<Object> list = restTemplate.getForObject(stateUrl, List.class, userId, sign(timestamp), timestamp);
	System.out.println(list.size());
    }

    private String sign(String timestamp) {
	return DigestUtils.md5DigestAsHex((userId + password + timestamp).getBytes());
    }

}
